﻿using Hidistro.Core;
using Hidistro.UI.Common.Controls;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using System.Net;
using System.Web;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_Pager : AscxTemplatedWebControl
    {
        private Repeater PList;
        public int mul;
        protected override void AttachChildControls()
        {
            this.PList = (Repeater)this.FindControl("PList");
            this.PList.ItemDataBound += PList_ItemDataBound;
            int start = 0;
            if (this.Index > 3) start = this.Index - 3;
            int len = 5;
            this.mul = (int)Math.Ceiling((decimal)this.Total / this.Size);
            if (this.mul < 5) len = this.mul;
            int x = this.Index + 2;
            x = x - this.mul;
            if (x > 0)
            {
                start -= x;
                if (start < 0) start = 0;
            }
            List<Info> list = new List<Info>();
            for (int i = start; i < start + len; i++)
            {
                Info info = new Info()
                {
                    Index = i + 1,
                    Selected = this.Index == i + 1
                };
                list.Add(info);
            }
            this.PList.DataSource = list;
            this.PList.DataBind();
        }
        void PList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                HtmlGenericControl div = (HtmlGenericControl)e.Item.FindControl("div");
                Info info = (Info)e.Item.DataItem;
                if (info.Selected)
                {
                    div.TagName = "a";
                    div.Attributes["Class"] = "current";
                    div.Attributes["href"] = this.CreateUrl("pn", info.Index.ToString());
                    div.Attributes["Index"] = info.Index.ToString();
                }
                else
                {
                    div.TagName = "a";
                    div.Attributes["href"] = this.CreateUrl("pn", info.Index.ToString());
                    div.Attributes["Index"] = info.Index.ToString();
                }

                if (string.IsNullOrEmpty(this.Page.Request.QueryString["Staticize"]))
                {
                    div.Attributes["href"] = this.CreateUrl("pn", info.Index.ToString());
                }
                else
                {
                    div.Attributes["href"] = this.CreateStaticizeUrl(info.Index);
                }

                div.InnerText = info.Index.ToString();
            }
        }
        protected override void OnInit(EventArgs e)
        {
            int pn;
            int.TryParse(this.Page.Request.QueryString["pn"], out pn);
            if (pn == 0) pn = 1;
            this.Index = pn;
            if (this.SkinName == null)
            {
                this.SkinName = "/ascx/tags/Common_GoodsList/wnxd_Pager.ascx";
            }
            base.OnInit(e);
        }
        private string CreateUrl(string paraName, string paraValue)
        {
            string rawUrl = this.Context.Request.RawUrl;
            if (rawUrl.IndexOf("?") >= 0)
            {
                string oldValue = rawUrl.Substring(rawUrl.IndexOf("?") + 1);
                string[] strArray = oldValue.Split(new char[] { Convert.ToChar("&") });
                rawUrl = rawUrl.Replace(oldValue, "");
                foreach (string str3 in strArray)
                {
                    if (!str3.ToLower().StartsWith(paraName + "="))
                    {
                        rawUrl = rawUrl + str3 + "&";
                    }
                }
                return (rawUrl + paraName + "=" + Globals.UrlEncode(paraValue));
            }
            string str4 = rawUrl;
            return (str4 + "?" + paraName + "=" + Globals.UrlEncode(paraValue));
        }
        public string CreateStaticizeUrl(int pos)
        {
            if (this.Page.Request.Path == "/SubCategory.aspx")
            { 
            int feature;
            int brand;
            int filter;
            int rank;

            int.TryParse(this.Page.Request.QueryString["feature"], out feature);
            int.TryParse(this.Page.Request.QueryString["brand"], out brand);
            int.TryParse(this.Page.Request.QueryString["filter"], out filter);
            int.TryParse(this.Page.Request.QueryString["rank"], out rank);

            int categoryId;
            int.TryParse(this.Page.Request.QueryString["categoryId"], out categoryId);

            CategoryInfo cateif = CategoryBrowser.GetCategory(categoryId);

            return Globals.GetSiteUrls().StaticizeSubCategory(cateif.CategoryId, cateif.RewriteName, (int)cateif.ParentCategoryId, cateif.ParentRewriteName, feature, brand, filter, rank, pos);
            }
            else if(this.Page.Request.Path == "/BrandDetails.aspx")
            {
               return StaticizeBrand(pos);
            }
            return "";
        }

        string StaticizeBrand(int pn)
        {
            //Brand_detail-{0}_cid-{1}_filter-{2}_rank-{3}.html
            int cid;
            int brand;
            int filter;
            int rank;

            //string model = "Brand_detail-{0}_cid-{1}_filter-{2}_rank-{3}.html";
            //string modelpn = "Brand_detail-{0}_cid-{1}_filter-{2}_rank-{3}_pn-{4}.html";
            string model = "B{0}C{1}F{2}R{3}.html";
            string modelpn = "B{0}C{1}F{2}R{3}P{4}.html";
            string url;
            int.TryParse(this.Page.Request.QueryString["cid"], out cid);
            int.TryParse(this.Page.Request.QueryString["brandId"], out brand);
            int.TryParse(this.Page.Request.QueryString["filter"], out filter);
            int.TryParse(this.Page.Request.QueryString["rank"], out rank);

            if ((pn == 1)||(pn ==0))
            {
                url = string.Format(model,brand,cid,filter,rank);
            }
            else
            {
                url = string.Format(modelpn, brand, cid, filter, rank,pn);
            }
            return url;
        }
        public int Index { get; set; }
        public int Size { get; set; }
        public int Total { get; set; }
        private class Info
        {
            public bool Selected { get; set; }
            public int Index { get; set; }
        }
    }
}
